package com.stanley.alogrithm


interface PepoleX {
    var familyName:String;
    fun say()
    fun printHello(){
        println("hello")
    }
}

fun String.sayYes():String {
    println("oh,yes!")
    return "yes"
}

class Person: PepoleX {
    lateinit var lastName:String

    var name:String? = "Ps"
    get() = field?.toUpperCase()
    set(value) {
        field = value?.toLowerCase()
    }
    init {
        lastName = "Stanley"
    }

    inner class InnerCls {
        fun getName() = name

        fun testInner() {
            
            var obj = this@Person
            lastName.sayYes();
            println("test Inner: ${obj.name}")
        }
    }
    class NestedCls {
        fun testNested() {
            println("test nested.")
        }

    }

    val sex:Boolean = false

    fun show() {
        println("Person: ${name}-Male:${sex}")
    }

    override var familyName: String
        get() = TODO("Not yet implemented")
        set(value) {}

    override fun say() {
        TODO("Not yet implemented")
    }
}

fun main(args:Array<String>) {

    var myName:String? = null
    var len:Int? = myName?.length
    println("len is ${myName?.length}")
    var a: Any? = null
    var nestedInstance = Person.NestedCls() // 相当于Java的静态内部类
    var innerInstance = Person().InnerCls() // 相当于Java的实例内部类
    innerInstance.testInner();

    var p:Person = Person()// kotlin中没有new关键字
    p.show()

    var hello = Hello("Michael", 30)
    hello.sayHello("k")
    var ageStr:String? = null
    val age1 = ageStr?.toInt()
    val age2 = ageStr?.toInt()?:-1


    val max = 100_2_3// 10023
    println("ageStr:$ageStr, age1 = $age1, age2=$age2, max=$max")
    for (i in 1..4) {
        println("i=" + i)
    }
}

class Hello constructor(var name:String, val age:Int) { // 参数和属性直接关联起来了
    init {
        //name = "Stanley", 默认参数修饰符为val，只读，不能修改
        println("init for class, property:$name")
    }
    fun sayHello(name:String) {
        println("Hello, ${this.name} !")
    }
}

